package fr.iut2.tc1.premierprogramme.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class PhotoShow extends Composite {
	private Label nomImg = new Label();
	private Image img;
	private Button buttonModifier = new Button("modifier");
	private TextBox textDescription = new TextBox();
	private VerticalPanel vPanel = new VerticalPanel();
	private HorizontalPanel hPanelAction = new HorizontalPanel();

	public PhotoShow(String photo){
		// Initialisation première image
		img = new Image(GWT.getHostPageBaseURL() + "images/"+photo);

		//private ArrayList <String> files = FileFactory.getFilesNames("/home/fournico/workspace/PremierProgramme/war/images");
				buttonModifier.addStyleName("pc-template-btn");
				// or we can set an id on a specific element for styling
				textDescription.getElement().setId("pc-template-textDescription");
				nomImg.getElement().setId("pc-template-nomImg");
				img.setTitle(FileFactory.getFilesNames("images").get(1));
				img.setAltText(FileFactory.getFilesNames("images").get(1));
				img.getElement().setId("pc-template-img");
				//specified text
				nomImg.setText(img.getTitle());
				textDescription.setText("Description");
				//specified width
				img.setWidth("300px");
				textDescription.setWidth("220px");
				vPanel.setWidth("300px");

				hPanelAction.add(textDescription);
				hPanelAction.add(buttonModifier);
				hPanelAction.getElement().setId("pc-template-hPanelAction");
				
				vPanel.add(nomImg);
				vPanel.add(img);
				vPanel.add(hPanelAction);

				// Add image and button to the RootPanel
				this.initWidget(vPanel);
				
				//ACTIONS
				buttonModifier.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent event) {
						if (!textDescription.getText().equals("") && !textDescription.getText().equals("Description")){
							img.setTitle(textDescription.getText());
							img.setAltText(textDescription.getText());
							nomImg.setText(textDescription.getText());
						}
					}
				});
				
	}
	public void setImg( Image img){
		//on crée un nouvel objet image car celle importé à été réduite
		Image image= new Image(img.getUrl());
		
		image.setWidth("300px");
		image.setTitle(img.getTitle());
		image.setAltText(img.getAltText());
		vPanel.remove(this.img);
		((Label)vPanel.getWidget(vPanel.getWidgetIndex(this.nomImg))).setText(img.getAltText());
		vPanel.insert(image, 1);
		this.img= image;
	}
	
}
